


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > PacManUI</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.ui</a>
</div>

<h1>Coverage Summary for Class: PacManUI (nl.tudelft.jpacman.ui)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">PacManUI</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/4)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/24)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package nl.tudelft.jpacman.ui;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import java.awt.BorderLayout;
<i class="no-highlight">4</i>&nbsp;import java.awt.Container;
<i class="no-highlight">5</i>&nbsp;import java.util.Map;
<i class="no-highlight">6</i>&nbsp;import java.util.concurrent.Executors;
<i class="no-highlight">7</i>&nbsp;import java.util.concurrent.ScheduledExecutorService;
<i class="no-highlight">8</i>&nbsp;import java.util.concurrent.TimeUnit;
<i class="no-highlight">9</i>&nbsp;
<i class="no-highlight">10</i>&nbsp;import javax.swing.JFrame;
<i class="no-highlight">11</i>&nbsp;import javax.swing.JPanel;
<i class="no-highlight">12</i>&nbsp;import javax.swing.WindowConstants;
<i class="no-highlight">13</i>&nbsp;
<i class="no-highlight">14</i>&nbsp;import nl.tudelft.jpacman.game.Game;
<i class="no-highlight">15</i>&nbsp;import nl.tudelft.jpacman.ui.ScorePanel.ScoreFormatter;
<i class="no-highlight">16</i>&nbsp;
<i class="no-highlight">17</i>&nbsp;/**
<i class="no-highlight">18</i>&nbsp; * The default JPacMan UI frame. The PacManUI consists of the following
<i class="no-highlight">19</i>&nbsp; * elements:
<i class="no-highlight">20</i>&nbsp; *
<i class="no-highlight">21</i>&nbsp; * &lt;ul&gt;
<i class="no-highlight">22</i>&nbsp; * &lt;li&gt;A score panel at the top, displaying the score of the player(s).
<i class="no-highlight">23</i>&nbsp; * &lt;li&gt;A board panel, displaying the current level, i.e. the board and all units
<i class="no-highlight">24</i>&nbsp; * on it.
<i class="no-highlight">25</i>&nbsp; * &lt;li&gt;A button panel, containing all buttons provided upon creation.
<i class="no-highlight">26</i>&nbsp; * &lt;/ul&gt;
<i class="no-highlight">27</i>&nbsp; *
<i class="no-highlight">28</i>&nbsp; * @author Jeroen Roosen 
<i class="no-highlight">29</i>&nbsp; *
<i class="no-highlight">30</i>&nbsp; */
<b class="nc"><i class="no-highlight">31</i>&nbsp;public class PacManUI extends JFrame {</b>
<i class="no-highlight">32</i>&nbsp;
<i class="no-highlight">33</i>&nbsp;    /**
<i class="no-highlight">34</i>&nbsp;     * Default serialisation UID.
<i class="no-highlight">35</i>&nbsp;     */
<i class="no-highlight">36</i>&nbsp;    private static final long serialVersionUID = 1L;
<i class="no-highlight">37</i>&nbsp;
<i class="no-highlight">38</i>&nbsp;    /**
<i class="no-highlight">39</i>&nbsp;     * The desired frame rate interval for the graphics in milliseconds, 40
<i class="no-highlight">40</i>&nbsp;     * being 25 fps.
<i class="no-highlight">41</i>&nbsp;     */
<i class="no-highlight">42</i>&nbsp;    private static final int FRAME_INTERVAL = 40;
<i class="no-highlight">43</i>&nbsp;
<i class="no-highlight">44</i>&nbsp;    /**
<i class="no-highlight">45</i>&nbsp;     * The panel displaying the player scores.
<i class="no-highlight">46</i>&nbsp;     */
<i class="no-highlight">47</i>&nbsp;    private final ScorePanel scorePanel;
<i class="no-highlight">48</i>&nbsp;
<i class="no-highlight">49</i>&nbsp;    /**
<i class="no-highlight">50</i>&nbsp;     * The panel displaying the game.
<i class="no-highlight">51</i>&nbsp;     */
<i class="no-highlight">52</i>&nbsp;    private final BoardPanel boardPanel;
<i class="no-highlight">53</i>&nbsp;
<i class="no-highlight">54</i>&nbsp;    /**
<i class="no-highlight">55</i>&nbsp;     * Creates a new UI for a JPacman game.
<i class="no-highlight">56</i>&nbsp;     *
<i class="no-highlight">57</i>&nbsp;     * @param game
<i class="no-highlight">58</i>&nbsp;     *            The game to play.
<i class="no-highlight">59</i>&nbsp;     * @param buttons
<i class="no-highlight">60</i>&nbsp;     *            The map of caption-to-action entries that will appear as
<i class="no-highlight">61</i>&nbsp;     *            buttons on the interface.
<i class="no-highlight">62</i>&nbsp;     * @param keyMappings
<i class="no-highlight">63</i>&nbsp;     *            The map of keyCode-to-action entries that will be added as key
<i class="no-highlight">64</i>&nbsp;     *            listeners to the interface.
<i class="no-highlight">65</i>&nbsp;     * @param scoreFormatter
<i class="no-highlight">66</i>&nbsp;     *            The formatter used to display the current score.
<i class="no-highlight">67</i>&nbsp;     */
<i class="no-highlight">68</i>&nbsp;    public PacManUI(final Game game, final Map&lt;String, Action&gt; buttons,
<i class="no-highlight">69</i>&nbsp;                    final Map&lt;Integer, Action&gt; keyMappings,
<i class="no-highlight">70</i>&nbsp;                    ScoreFormatter scoreFormatter) {
<b class="nc"><i class="no-highlight">71</i>&nbsp;        super(&quot;JPacman&lt;2022&gt;&quot;);</b>
<b class="nc"><i class="no-highlight">72</i>&nbsp;        assert game != null;</b>
<b class="nc"><i class="no-highlight">73</i>&nbsp;        assert buttons != null;</b>
<b class="nc"><i class="no-highlight">74</i>&nbsp;        assert keyMappings != null;</b>
<i class="no-highlight">75</i>&nbsp;
<b class="nc"><i class="no-highlight">76</i>&nbsp;        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);</b>
<i class="no-highlight">77</i>&nbsp;
<b class="nc"><i class="no-highlight">78</i>&nbsp;        PacKeyListener keys = new PacKeyListener(keyMappings);</b>
<b class="nc"><i class="no-highlight">79</i>&nbsp;        addKeyListener(keys);</b>
<i class="no-highlight">80</i>&nbsp;
<b class="nc"><i class="no-highlight">81</i>&nbsp;        JPanel buttonPanel = new ButtonPanel(buttons, this);</b>
<i class="no-highlight">82</i>&nbsp;
<b class="nc"><i class="no-highlight">83</i>&nbsp;        scorePanel = new ScorePanel(game.getPlayers());</b>
<b class="nc"><i class="no-highlight">84</i>&nbsp;        if (scoreFormatter != null) {</b>
<b class="nc"><i class="no-highlight">85</i>&nbsp;            scorePanel.setScoreFormatter(scoreFormatter);</b>
<i class="no-highlight">86</i>&nbsp;        }
<i class="no-highlight">87</i>&nbsp;
<b class="nc"><i class="no-highlight">88</i>&nbsp;        boardPanel = new BoardPanel(game);</b>
<i class="no-highlight">89</i>&nbsp;
<b class="nc"><i class="no-highlight">90</i>&nbsp;        Container contentPanel = getContentPane();</b>
<b class="nc"><i class="no-highlight">91</i>&nbsp;        contentPanel.setLayout(new BorderLayout());</b>
<b class="nc"><i class="no-highlight">92</i>&nbsp;        contentPanel.add(buttonPanel, BorderLayout.SOUTH);</b>
<b class="nc"><i class="no-highlight">93</i>&nbsp;        contentPanel.add(scorePanel, BorderLayout.NORTH);</b>
<b class="nc"><i class="no-highlight">94</i>&nbsp;        contentPanel.add(boardPanel, BorderLayout.CENTER);</b>
<i class="no-highlight">95</i>&nbsp;
<b class="nc"><i class="no-highlight">96</i>&nbsp;        pack();</b>
<i class="no-highlight">97</i>&nbsp;    }
<i class="no-highlight">98</i>&nbsp;
<i class="no-highlight">99</i>&nbsp;    /**
<i class="no-highlight">100</i>&nbsp;     * Starts the &quot;engine&quot;, the thread that redraws the interface at set
<i class="no-highlight">101</i>&nbsp;     * intervals.
<i class="no-highlight">102</i>&nbsp;     */
<i class="no-highlight">103</i>&nbsp;    public void start() {
<b class="nc"><i class="no-highlight">104</i>&nbsp;        setVisible(true);</b>
<b class="nc"><i class="no-highlight">105</i>&nbsp;        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();</b>
<b class="nc"><i class="no-highlight">106</i>&nbsp;        service.scheduleAtFixedRate(this::nextFrame, 0, FRAME_INTERVAL, TimeUnit.MILLISECONDS);</b>
<i class="no-highlight">107</i>&nbsp;    }
<i class="no-highlight">108</i>&nbsp;
<i class="no-highlight">109</i>&nbsp;    /**
<i class="no-highlight">110</i>&nbsp;     * Draws the next frame, i.e. refreshes the scores and game.
<i class="no-highlight">111</i>&nbsp;     */
<i class="no-highlight">112</i>&nbsp;    private void nextFrame() {
<b class="nc"><i class="no-highlight">113</i>&nbsp;        boardPanel.repaint();</b>
<b class="nc"><i class="no-highlight">114</i>&nbsp;        scorePanel.refresh();</b>
<i class="no-highlight">115</i>&nbsp;    }
<i class="no-highlight">116</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-05-22 18:55</div>
</div>
</body>
</html>
